package com.wangfugui.mybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangfugui.mybatisplus.common.PageResult;
import com.wangfugui.mybatisplus.common.Result;
import com.wangfugui.mybatisplus.controller.dto.CategoryQueryDTO;
import com.wangfugui.mybatisplus.dao.ShortPlayCategoriesMapper;
import com.wangfugui.mybatisplus.entity.ShortPlayCategories;
import com.wangfugui.mybatisplus.service.ShortPlayCategoriesService;
import com.wangfugui.mybatisplus.service.impl.BaseServiceImpl;
import com.wangfugui.mybatisplus.service.impl.ShortPlayCategoriesServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 二级分类表 前端控制器
 * </p>
 *
 * @author liyu
 * @since 2025-04-12
 */
@RestController
@RequestMapping("/shortPlayCategories")
@Api(value = "/shortPlayCategories", tags = "分类管理")
public class ShortPlayCategoriesController
        extends BaseController<ShortPlayCategoriesService, ShortPlayCategories> {

    @Autowired
    private ShortPlayCategoriesService service;

    @GetMapping("/query")
    public Result<List<ShortPlayCategories>> list(){
       return Result.ofSuccess(service.list());
    }

    @GetMapping("/page")
    @ApiOperation("分页查询分类")
    public Result<PageResult<ShortPlayCategories>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ShortPlayCategories> page = new Page<>(pageNum, pageSize);
        IPage<ShortPlayCategories> pageResult = service.page(page);
        return Result.ofSuccess(new PageResult<>(pageResult));
    }

    @PostMapping("/pageCondition")
    @ApiOperation("多条件分页查询")
    public Result<PageResult<ShortPlayCategories>> pageWithCondition(
            @RequestBody CategoryQueryDTO queryDTO,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<ShortPlayCategories> page = new Page<>(pageNum, pageSize);

        IPage<ShortPlayCategories> result = ((BaseServiceImpl<ShortPlayCategoriesMapper, ShortPlayCategories>)service)
                .pageWithCondition(page, wrapper -> {
                    // 1. 精确匹配条件
                    if (queryDTO.getParentId() != null) {
                        wrapper.eq(ShortPlayCategories::getParentId, queryDTO.getParentId());
                    }

                    // 2. 模糊查询
                    if (StringUtils.isNotBlank(queryDTO.getKeyword())) {
                        wrapper.and(w -> w
                                .like(ShortPlayCategories::getName, queryDTO.getKeyword()));
                    }

                    // 3. 状态过滤
                    if (queryDTO.getStatus() != null) {
                        wrapper.eq(ShortPlayCategories::getStatus, queryDTO.getStatus());
                    }
                    return wrapper;
                });

        return successPage(result);
    }
}
